package pe.edu.upc.dew.pharma.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.displaytag.tags.TableTagParameters;
import org.displaytag.util.ParamEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pe.edu.upc.dew.pharma.util.Constante;

@Controller
@RequestMapping("/paginar.do")
public class PaginacionController {

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView showPage(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("idDisplayTag")String idDisplayTag) {
		HttpSession session = request.getSession(false);
		if(session.getAttribute(Constante.OBJECT_MODEL_AND_VIEW) == null){
			throw new RuntimeException("el atributo '"+Constante.OBJECT_MODEL_AND_VIEW+"' en sesion es nulo.");
		}
		ModelAndView mav = (ModelAndView) session.getAttribute(Constante.OBJECT_MODEL_AND_VIEW);
		ParamEncoder paramEncoder = new ParamEncoder(idDisplayTag);
		String encodedParameterPage = paramEncoder.encodeParameterName(TableTagParameters.PARAMETER_PAGE);
		String parameterPage = request.getParameter(encodedParameterPage);
		mav.addObject(encodedParameterPage, parameterPage);
		return mav;
	}

}
